<?php
#user model
if(!defined('_CONST_VERSION'))
{
	die("Access Exit!");
}

/**
 * 获取投票
 */
 function get_vote_by_id($id)
 {
	global $database;
	$rs = $database->queryFirst("SELECT v.*, u.name, ui.avatar FROM vote v LEFT JOIN user u ON u.id = v.user_id LEFT JOIN user_info ui ON ui.user_id = v.user_id WHERE v.id = " .$id);
	return $rs; 
 }
 
/**
 *	增加投票
 */
function add_views($id)
{
	global $database;
	$database->execute("UPDATE vote SET views = views + 1 WHERE id = " .$id);
}

/**
 *  获得评论
 */
function get_comment_by_voteid($id)
{
	global $database;
	$rs = $database->queryArray("SELECT vc.*, ui.avatar FROM vote_comment vc LEFT JOIN user_info ui ON ui.user_id = vc.user_id  WHERE vc.vote_id = " .$id . " ORDER BY vc.id DESC LIMIT 5");
	return $rs;
}

/**
 * 获得投票总数
 */
function get_commentcount_by_voteid($id)
{
	global $database;
	$rs = $database->queryFirst("SELECT count(*) as total FROM vote_comment WHERE vote_id = " .$id);
	return $rs['total'];
}
 
/**
 * 获得热门投票
 */
 function get_hot_vote()
 {
	global $database;
	$rs = $database->queryArray("SELECT v.*, u.name, ui.avatar FROM vote v LEFT JOIN user u ON u.id = v.user_id LEFT JOIN user_info ui ON ui.user_id = v.user_id ORDER BY views DESC LIMIT 9");
	return $rs; 
 }

/**
 * 获得首页投票
 */
 function get_all_vote($page)
 {
	global $database;
	$rs = $database->queryArray("SELECT v.*, u.name, ui.avatar "
	                            ."FROM vote v LEFT JOIN user u ON u.id = v.user_id "
								."LEFT JOIN user_info ui ON ui.user_id = v.user_id "
								."ORDER BY create_time DESC "
								."LIMIT ".(($page - 1) * 50).", 10");
	return $rs; 
 }
 
/**
 * 获得首页投票总数
 */
 function get_all_vote_count()
 {
	global $database;
	$rs = $database->queryFirst("SELECT count(*) as total FROM vote");
	return $rs['total'];
 }
  
 
/**
 * 获得用户投票
 */
 function get_user_vote($id)
 {
	global $database;
	$rs = $database->queryFirst("SELECT `value` FROM user_votes WHERE vote_id = " . $id . " AND user_id = " .$_SESSION['userid']);
	return $rs;
 }
 
 /**
  * 获取某个用户商品投票总数
  */
  function get_user_vote_count($uid)
  {
	global $database;
	$rs = $database->queryFirst("SELECT count(*) as total FROM vote WHERE user_id=".$uid);
	return $rs['total'];
  }

 /**
  * 获取某个用户商品你投票
  */
  function get_user_vote_by_uid($uid, $page)
  {
	global $database;
	$rs = $database->queryArray("SELECT v.*, u.name, ui.avatar "
							."FROM vote v LEFT JOIN user u ON u.id = v.user_id "
							."LEFT JOIN user_info ui ON ui.user_id = v.user_id "
							."WHERE v.user_id=".$uid." "
							."ORDER BY create_time DESC "
							."LIMIT ".(($page - 1) * 50).", 10");
	return $rs; 
  }
?>